package com.mylove.judas.yaya.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.mylove.judas.yaya.R;
import com.mylove.judas.yaya.entity.ImageEntity;
import com.mylove.judas.yaya.mvp.model.MeiTuModel;
import com.mylove.judas.yaya.mvp.presenter.contract.MeiTuContract;
import com.mylove.judas.yaya.mvp.presenter.impl.MeiTuPresenter;
import com.mylove.judas.yaya.ui.adapter.MeiTuListAdapter;
import com.mylove.judas.yaya.ui.fragment.base.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Judas on 2017/6/10.
 */

public class MyPublishFragment extends BaseFragment<MeiTuPresenter> implements MeiTuContract.MeiTuView{

    private static final String TAG = MyPublishFragment.class.getSimpleName();

    private int pageIndex = 1;
    private int pageSize = 1;

    private SmartRefreshLayout srl;
    private RecyclerView rv;
    private List<ImageEntity> list = new ArrayList<>();
    private MeiTuListAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my_publish;
    }


    @Override
    public void initView() {
        super.initView();

        presenter = new MeiTuPresenter(new MeiTuModel(), this);

        srl = (SmartRefreshLayout) findViewById(R.id.srl);
        rv = (RecyclerView) findViewById(R.id.rv);

        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        rv.setLayoutManager(manager);

        adapter = new MeiTuListAdapter(mContext);
        adapter.setList(list);
        rv.setAdapter(adapter);


    }

    @Override
    public void setListener() {
        super.setListener();

        srl.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.getImageAll(pageIndex);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                list.clear();
                adapter.notifyDataSetChanged();
                presenter.getImageAll(pageIndex);
            }
        });

    }

    public void show(){
        if(pageIndex ==1){
            presenter.getImageAll(pageIndex);
        }
    }


    @Override
    public void showListResult(List<ImageEntity> list) {

        pageIndex ++;
        if(srl.isRefreshing()){
            srl.finishRefresh();
        }else if(srl.isLoading()){
            srl.finishLoadmore();

            if(list.size() < pageSize){
                srl.setLoadmoreFinished(true);
            }

        }
        ImageEntity ie = new ImageEntity();
        if(list.size() == pageSize){
            list.add(9, ie);
            list.add(ie);
        }else if(list.size() >= 10){
            list.add(ie);
        }

        this.list.addAll(list);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void showNoData() {
        super.showNoData();

        if(srl.isRefreshing()){
            srl.finishRefresh();
        }else if(srl.isLoading()){
            srl.finishLoadmore();
        }

    }

}
